<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="nav-bar">
				<view class="nav-left" @click="goBack">
					<view class="back-arrow"></view>
				</view>
				<view class="nav-title">就近加油</view>
				<view class="nav-right"></view>
			</view>

			<!-- 搜索栏 -->
			<view class="search-wrapper">
				<view class="search-box">
					<input class="search-input" placeholder="输入需要搜索的内容" placeholder-style="color: #999;" />
					<image class="search-icon" src="/static/image/refuelNearby/搜索.png" mode="aspectFit"></image>
				</view>
				<view class="map-mode-btn" @click="toggleViewMode">
					<image class="map-mode-bg" src="/static/image/refuelNearby/组 5.png" mode="aspectFill"></image>
					<view class="map-mode-content">
						<text class="map-text">{{ viewMode === 'list' ? '地图' : '列表' }}</text>
						<text class="map-text">模式</text>
					</view>
				</view>
			</view>

			<!-- 油品选择（仅列表模式显示） -->
			<view class="oil-types" :class="{ 'oil-types-hidden': viewMode === 'map' }">
				<view v-for="(type, index) in oilTypes" :key="index"
					:class="['oil-type-btn', { 'active': selectedType === type }]" @click="selectOilType(type)">
					{{ type }}
				</view>
			</view>
		</view>

		<!-- 加油站列表视图 -->
		<scroll-view v-if="viewMode === 'list'" class="station-list" scroll-y @touchmove.stop>
			<view v-for="(station, index) in stationList" :key="index" class="station-card"
				@click="goToStoreDetails(station)">
				<view class="station-header">
					<text class="station-name">{{ station.name }}</text>
					<text class="station-distance">{{ station.distance }}</text>
				</view>

				<view class="discount-tags">
					<text v-for="(tag, idx) in station.discounts" :key="idx" class="discount-tag">
						{{ tag }}
					</text>
				</view>

				<view class="station-content">
					<image class="station-img" :src="station.image" mode="aspectFill"></image>
					<view class="station-info">
						<view class="info-row">
							<text class="info-label">营业时间：</text>
							<text class="info-value">{{ station.businessTime }}</text>
						</view>
						<view class="price-row">
							<text class="price">¥ {{ station.price }}/升</text>
						</view>
						<view class="address-row">
							<view class="address-content">
								<text class="address">{{ station.address }}</text>
								<image class="nav-icon" src="/static/image/refuelNearby/plane.png" mode="aspectFit" @click.stop="navigateToStation(station)"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 地图视图 -->
		<view v-if="viewMode === 'map'" class="map-view">
			<!-- 地图和信息整合卡片 -->
			<view class="map-card-container">
				<!-- 地图区域 -->
				<view class="map-wrapper">
					<map class="map" :latitude="mapCenter.latitude" :longitude="mapCenter.longitude" :markers="markers"
						:scale="13" @markertap="onMarkerTap" show-location></map>
				</view>

				<!-- 底部加油站信息 -->
				<view v-if="selectedStation" class="map-info-bottom">
					<view class="map-info-row">
						<view class="map-info-left">
							<text class="map-info-name">{{ selectedStation.name }}</text>
							<text class="map-info-address">{{ selectedStation.address }}</text>
							<view class="map-detail-link" @click.stop="goToStoreDetails(selectedStation)">
								<text class="map-detail-icon">ⓘ</text>
								<text class="map-detail-text">门店详情</text>
								<text class="map-arrow" :decode="true">&gt;&gt;</text>
							</view>
						</view>
						<image class="map-info-img" :src="selectedStation.image" mode="aspectFill"></image>
					</view>
					<view class="map-navigate-btn" @click="navigate">
						<text class="map-navigate-text">立即导航</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';

	// 视图模式：list-列表模式，map-地图模式
	const viewMode = ref('list');

	// 油品类型
	const oilTypes = ref(['90#', '92#', '93#', '95#', '97#', '98#']);
	const selectedType = ref('92#');

	// 选中的加油站（地图模式下使用）
	const selectedStation = ref(null);

	// 加油站列表数据（添加经纬度信息）
	const stationList = ref([{
			id: 1,
			name: '中国石油加油站',
			distance: '556m',
			discounts: ['满200减20', '满500减60'],
			image: '/static/image/图层 1.png',
			businessTime: '周一至周五 早上8:00-晚上22:00',
			price: '5.66',
			address: '山阳区人民路',
			latitude: 35.2236,
			longitude: 113.2925,
			phone: '0391-1234567'
		},
		{
			id: 2,
			name: '中国石油加油站',
			distance: '880m',
			discounts: ['满200减20', '满500减60'],
			image: '/static/image/图层 1.png',
			businessTime: '周一至周五 早上8:00-晚上22:00',
			price: '5.66',
			address: '山阳区人民路与解放路交叉口',
			latitude: 35.2156,
			longitude: 113.2845,
			phone: '0391-1234567'
		},
		{
			id: 3,
			name: '中国石油加油站',
			distance: '1.2km',
			discounts: ['满200减20', '满500减60'],
			image: '/static/image/图层 1.png',
			businessTime: '周一至周五 早上8:00-晚上22:00',
			price: '5.66',
			address: '山阳区人民路与解放路交叉口',
			latitude: 35.2286,
			longitude: 113.3005,
			phone: '0391-1234567'
		},
		{
			id: 4,
			name: '中国石油加油站',
			distance: '1.5km',
			discounts: ['满200减20', '满500减60'],
			image: '/static/image/图层 1.png',
			businessTime: '周一至周五 早上8:00-晚上22:00',
			price: '5.66',
			address: '山阳区人民路与解放路交叉口',
			latitude: 35.2106,
			longitude: 113.2795,
			phone: '0391-1234567'
		}
	]);

	// 地图标记点数据
	const markers = ref([]);

	// 地图中心点（焦作市中心）
	const mapCenter = ref({
		latitude: 35.2236,
		longitude: 113.2925
	});

	// 初始化地图标记点
	const initMarkers = () => {
		markers.value = stationList.value.map((station, index) => ({
			id: station.id,
			latitude: station.latitude,
			longitude: station.longitude,
			iconPath: '/static/image/定位2.png',
			width: 30,
			height: 30,
			callout: {
				content: station.name,
				display: 'ALWAYS',
				fontSize: 12,
				borderRadius: 5,
				padding: 5,
				color: '#333',
				bgColor: '#fff'
			}
		}));
		// 默认选中第一个
		if (stationList.value.length > 0) {
			selectedStation.value = stationList.value[0];
		}
	};

	// 选择油品类型
	const selectOilType = (type) => {
		selectedType.value = type;
	};

	// 跳转到门店详情
	const goToStoreDetails = (station) => {
		uni.navigateTo({
			url: `/pages/index/refuelNearby/storeDetails/storeDetails?name=${encodeURIComponent(station.name)}&address=${encodeURIComponent(station.address)}&businessTime=${encodeURIComponent(station.businessTime)}&image=${encodeURIComponent(station.image)}&latitude=${station.latitude}&longitude=${station.longitude}&phone=${encodeURIComponent(station.phone)}`
		});
	};

	// 切换视图模式
	const toggleViewMode = () => {
		if (viewMode.value === 'list') {
			viewMode.value = 'map';
			initMarkers();
		} else {
			viewMode.value = 'list';
		}
	};

	// 点击地图标记点
	const onMarkerTap = (e) => {
		const markerId = e.detail.markerId;
		const station = stationList.value.find(s => s.id === markerId);
		if (station) {
			selectedStation.value = station;
		}
	};

	// 导航到指定加油站
	const navigateToStation = (station) => {
		if (station) {
			uni.navigateTo({
				url: `/pages/index/refuelNearby/navigation/navigation?name=${encodeURIComponent(station.name)}&address=${encodeURIComponent(station.address)}&latitude=${station.latitude}&longitude=${station.longitude}`
			});
		}
	};

	// 立即导航（地图模式使用）
	const navigate = () => {
		if (selectedStation.value) {
			navigateToStation(selectedStation.value);
		}
	};

	// 返回
	const goBack = () => {
		uni.navigateBack();
	};
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		background: #f5f5f5;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	/* 头部区域 */
	.header {
		background: linear-gradient(180deg, #2477F4 0%, #166BEC 100%);
		padding-top: 30rpx;
		padding-bottom: 30rpx;
	}

	/* 导航栏 */
	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		padding-top: calc(20rpx + env(safe-area-inset-top));
	}

	.nav-left {
		width: 80rpx;
		display: flex;
		align-items: center;
		cursor: pointer;
	}

	.back-arrow {
		width: 24rpx;
		height: 24rpx;
		border-left: 4rpx solid #fff;
		border-bottom: 4rpx solid #fff;
		transform: rotate(45deg);
		margin-left: 10rpx;
	}

	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 36rpx;
		font-weight: 500;
		color: #fff;
	}

	.nav-right {
		width: 80rpx;
	}

	/* 搜索区域 */
	.search-wrapper {
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		margin-top: 20rpx;
		gap: 20rpx;
	}

	.search-box {
		flex: 1;
		background: #fff;
		border-radius: 50rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		position: relative;
	}

	.search-input {
		flex: 1;
		font-size: 28rpx;
		height: 100%;
	}

	.search-icon {
		width: 36rpx;
		height: 36rpx;
		flex-shrink: 0;
	}

	.map-mode-btn {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		position: relative;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(255, 107, 53, 0.4);
		cursor: pointer;
		transition: transform 0.2s;
	}

	.map-mode-btn:active {
		transform: scale(0.95);
	}

	.map-mode-bg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 1;
	}

	.map-mode-content {
		position: relative;
		z-index: 2;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.map-text {
		color: #fff;
		font-size: 24rpx;
		line-height: 32rpx;
		font-weight: 500;
	}

	/* 油品选择 */
	.oil-types {
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		gap: 20rpx;
		height: 130rpx;
		box-sizing: border-box;
	}

	/* 地图模式下隐藏但保持占位 */
	.oil-types-hidden {
		visibility: hidden;
		pointer-events: none;
	}

	.oil-type-btn {
		flex: 1;
		height: 70rpx;
		border: 2rpx solid rgba(255, 255, 255, 0.6);
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		font-size: 30rpx;
		font-weight: 500;
		transition: all 0.3s;
	}

	.oil-type-btn.active {
		background: rgba(255, 255, 255, 0.3);
		border-color: #fff;
	}

	/* 加油站列表 */
	.station-list {
		flex: 1;
		padding: 20rpx 30rpx;
		height: 0;
	}

	.station-card {
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
		box-sizing: border-box;
		width: 690rpx;
	}

	.station-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.station-name {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.station-distance {
		font-size: 28rpx;
		color: #4A90E2;
		font-weight: 500;
	}

	.discount-tags {
		display: flex;
		gap: 15rpx;
		margin-bottom: 25rpx;
	}

	.discount-tag {
		padding: 8rpx 20rpx;
		background: #FFF5F0;
		border: 1rpx solid #FF6B35;
		border-radius: 8rpx;
		color: #FF6B35;
		font-size: 24rpx;
	}

	.station-content {
		display: flex;
		gap: 20rpx;
		overflow: hidden;
	}

	.station-img {
		width: 200rpx;
		height: 150rpx;
		border-radius: 12rpx;
		flex-shrink: 0;
	}

	.station-info {
		flex: 1;
		min-width: 0;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		overflow: hidden;
	}

	.info-row {
		display: flex;
		align-items: center;
		overflow: hidden;
	}

	.info-label {
		font-size: 26rpx;
		color: #666;
		flex-shrink: 0;
	}

	.info-value {
		font-size: 26rpx;
		color: #333;
		flex-shrink: 0;
	}

	.price-row {
		margin: 10rpx 0;
		overflow: hidden;
	}

	.price {
		font-size: 36rpx;
		color: #FF6B35;
		font-weight: 600;
		white-space: nowrap;
	}

	.address-row {
		display: flex;
		align-items: center;
	}

	.address-content {
		flex: 1;
		display: flex;
		align-items: center;
		min-width: 0;
	}

	.address {
		font-size: 24rpx;
		color: #666;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		margin-right: 30rpx;
		flex-shrink: 1;
	}

	.nav-icon {
		width: 32rpx;
		height: 32rpx;
		flex-shrink: 0;
	}

	/* 图标字体 */
	.iconfont {
		font-family: 'customicons';
	}

	/* 地图视图 */
	.map-view {
		flex: 1;
		padding: 0 30rpx 20rpx 30rpx;
		margin-top: -130rpx;
		height: 0;
		overflow: hidden;
	}

	/* 地图和信息整合卡片容器 */
	.map-card-container {
		width: 100%;
		height: calc(100% + 130rpx);
		background: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		box-sizing: border-box;
	}

	/* 地图区域包装器 */
	.map-wrapper {
		height: 800rpx;
		position: relative;
		overflow: hidden;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		flex-shrink: 0;
	}

	.map {
		width: 100%;
		height: 100%;
	}

	/* 底部加油站信息区域 */
	.map-info-bottom {
		background: #fff;
		padding: 0;
	}

	.map-info-row {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
		margin-bottom: 20rpx;
	}

	.map-info-left {
		flex: 1;
		display: flex;
		flex-direction: column;
		gap: 10rpx;
		min-width: 0;
	}

	.map-info-name {
		font-size: 34rpx;
		font-weight: 700;
		color: #000;
		line-height: 1.2;
	}

	.map-info-address {
		font-size: 26rpx;
		color: #666;
		line-height: 1.5;
	}

	.map-detail-link {
		display: flex;
		align-items: center;
		gap: 6rpx;
		margin-top: 4rpx;
	}

	.map-detail-icon {
		font-size: 28rpx;
		color: #2477F4;
	}

	.map-detail-text {
		font-size: 26rpx;
		color: #2477F4;
		font-weight: 500;
	}

	.map-arrow {
		font-size: 26rpx;
		color: #2477F4;
		margin-left: 4rpx;
	}

	.map-info-img {
		width: 200rpx;
		height: 140rpx;
		border-radius: 12rpx;
		flex-shrink: 0;
	}

	.map-navigate-btn {
		width: 100%;
		height: 80rpx;
		background: linear-gradient(135deg, #2477F4 0%, #166BEC 100%);
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 4rpx 12rpx rgba(36, 119, 244, 0.25);
	}

	.map-navigate-text {
		color: #fff;
		font-size: 32rpx;
		font-weight: 600;
		letter-spacing: 2rpx;
	}
</style>